package com.JavaBasic;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GUIDemo {
	public static void main(String[] args) {
		// 创建一个窗口
		Frame fm = new Frame("我是窗口标题");
		fm.setSize(500, 500);
		fm.setLocation(400, 100);
		fm.setVisible(true);
		
		// 在窗口上添加一个按钮
		Button bt = new Button("按钮1");
		bt.setSize(100, 50);
		bt.setLocation(200, 40);
		fm.add(bt);
		fm.setLayout(new FlowLayout());
		
		// 添加另外一个按钮
		Button bt2 = new Button("按钮2");
		bt2.setSize(100, 50);
		bt2.setLocation(bt.getLocation().x + bt.getSize().width + 20, bt.getLocation().y);
		fm.add(bt2);
		
		
		// 添加WindowListener
		fm.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// 点击关闭按钮，关闭java虚拟机
				System.exit(0);
			}
		});
		
		// 添加鼠标点击时间
		bt.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				// 按钮点击关闭java虚拟机
				System.exit(0);
			}
		});
		
		// 添加键盘事件
		bt2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 32) {
					// 点击空格
					System.out.println("点击空格");
				}
			}
		});
		
		
	}
}
